package com.yoomiito.app.ui.anyview.videoclip

import android.content.Context
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.yoomiito.app.R
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.utils.DensityUtil
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

class RollingMsgView(val context: BaseActivity<*>) : androidx.recyclerview.widget.RecyclerView(context) {
    private val mAdapter = RcyAdapter(context)
    private var disposable: Disposable? = null
    private var scrollDisposable: Disposable? = null
    private var mTop: Int = -1
    private var mSumHeight = 0
    private var isScroll = false
    private var datas: MutableList<String>? = null
    private var mSize = 0
    private var firstMesure = true

    init {
        layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context)
        itemAnimator = androidx.recyclerview.widget.DefaultItemAnimator()
        (itemAnimator as androidx.recyclerview.widget.DefaultItemAnimator).moveDuration = 2000
        adapter = mAdapter

    }

    fun setNewDatas(datas: MutableList<String>) {
        cancel()
        this.datas?.clear()
        mAdapter.notifyDataSetChanged()
        this.datas = datas
        mAdapter.notifyDataSetChanged()
        firstMesure = true
        isScroll = false
        mSumHeight = 0
        mSize = datas.size

        if (datas.size != 0) {
            disposable = Observable.interval(2, TimeUnit.SECONDS)
                    .compose(context.bindToLifecycle())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe {
                        if (null == getChildAt(0)) return@subscribe
                        if (!isScroll && firstMesure) {
                            if (datas.size >= 4) {
                                isScroll = true
                                datas.addAll(datas)
                                mAdapter.notifyDataSetChanged()
                            } else {
                                val size = datas.size
                                for (i in 0 until size) {
                                    val childView = getChildAt(i) ?: return@subscribe
                                    mSumHeight += childView.measuredHeight
                                    if (mSumHeight >= DensityUtil.dp2px(68f)) {
                                        isScroll = true
                                        datas.addAll(datas)
                                        mAdapter.notifyDataSetChanged()
                                        break
                                    }
                                }
                                mSumHeight = 0
                            }

                        }
                        if (isScroll) {
                            var position = it.toInt()
                            val tv = getChildAt(0)
//                            val tv1 = tv.findViewById<TextView>(R.id.tv)
//                            L.i("TV：${tv1.text}")
                            if (position + 1 > mSize) {

                                position %= mSize
                            }
                            if (tv != null) {

                                mTop = tv?.height!!
                                smoothScrollBy(0, mTop)
                            }
                            if (position == mSize - 1) {

                                scrollDisposable = Observable.timer(1, TimeUnit.SECONDS)
                                        .subscribeOn(Schedulers.io())
                                        .observeOn(AndroidSchedulers.mainThread())
                                        .subscribe {
                                            //                                    smoothScrollToPosition(0)
//                                    datas.addAll(datas)
//                                    mAdapter.notifyDataSetChanged()
//                                    scrollDisposable?.dispose()

                                            scrollToPosition(0)
                                        }
                            }
                        }
                    }
        }
    }

    fun cancel() {
        if (null != disposable) {
            disposable?.dispose()
            disposable = null
        }
    }


    override fun onInterceptTouchEvent(e: MotionEvent?): Boolean {
        return true
    }


    inner class RcyAdapter(val context: Context) : androidx.recyclerview.widget.RecyclerView.Adapter<ViewHolder>() {

        override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
            val tv = LayoutInflater.from(context).inflate(R.layout.item_tv_3, p0, false)
            return ItemViewHolder(tv)
        }

        override fun getItemCount(): Int = if (null == datas) 0 else datas?.size!!

        override fun onBindViewHolder(p0: ViewHolder, p1: Int) {
            val holder = p0 as ItemViewHolder
            holder.tv.text = datas?.get(p1)!!
        }
    }

    class ItemViewHolder(view: View) : ViewHolder(view) {
        var tv = itemView.findViewById<TextView>(R.id.tv)
    }
}